﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Util;
using Repository.Util;
using Xproject.Util;
using System.Data;
using Model.Util.DTO;

namespace Services.Util
{
    public class CategoryServices : Service
    {
        IRepositoryAsync<Category> m_CategoryRepository;
        IRepositoryAsync<CategoryAttr> m_CategoryAttrRepository;
        IRepositoryAsync<CategoryAttrGroups> m_CategoryAttrGroupsRepository;
        IRepositoryAsync<CategoryAttrValues> m_CategoryAttrValuesRepository;

        public CategoryServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_CategoryRepository = m_UnitOfWork.RepositoryAsync<Category>();
            m_CategoryAttrRepository = m_UnitOfWork.RepositoryAsync<CategoryAttr>();
            m_CategoryAttrGroupsRepository = m_UnitOfWork.RepositoryAsync<CategoryAttrGroups>();
            m_CategoryAttrValuesRepository = m_UnitOfWork.RepositoryAsync<CategoryAttrValues>();
        }

        public   List<Category> GetAllCategory()
        {            
            if (Cache_Category.CategoryList == null)
            {
                List<Category> sourceCategoryList = m_CategoryRepository.GetCategoryList();
                List<Category> categoryList = new List<Category>();
                CreateCategoryTree(sourceCategoryList, categoryList, 0);
                Cache_Category.CategoryList = categoryList;
            }
            return Cache_Category.CategoryList;
            
        }

        

        /// <summary>
        /// 通过分类名称获得分类id
        /// </summary>
        /// <param name="name">分类名称</param>
        /// <returns></returns>
        public    int GetCateIdByName(string name)
        {
            foreach (Category categoryInfo in GetAllCategory())
            {
                if (categoryInfo.Name == name)
                    return categoryInfo.CategoryID;
            }
            return 0;
        }

        /// <summary>
        /// 获得分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public   Category GetCategoryById(int cateId)
        {
            foreach (Category categoryInfo in GetAllCategory())
            {
                if (categoryInfo.CategoryID == cateId)
                    return categoryInfo;
            }

            return null;
        }

        /// <summary>
        /// 递归创建分类列表树
        /// </summary>
        protected    void CreateCategoryTree(List<Category> sourceCategoryList, List<Category> resultCategoryList, int parentId)
        {
            foreach (Category categoryInfo in sourceCategoryList)
            {
                if (categoryInfo.ParentId == parentId)
                {
                    resultCategoryList.Add(categoryInfo);
                    CreateCategoryTree(sourceCategoryList, resultCategoryList, categoryInfo.CategoryID);
                }
            }
        }

        /// <summary>
        /// 获得子分类列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="layer">级别</param>
        /// <returns></returns>
        public  List<Category> GetChildCategoryList(int cateId, int layer)
        {
            return GetChildCategoryList(cateId, layer, false);
        }

        /// <summary>
        /// 获得子分类列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="layer">级别</param>
        /// <param name="isAllChildren">是否包括全部子节点</param>
        /// <returns></returns>
        public   List<Category> GetChildCategoryList(int cateId, int layer, bool isAllChildren)
        {
            List<Category> categoryList = new List<Category>();

            if (cateId > 0)
            {
                bool flag = false;
                if (isAllChildren)
                {
                    foreach (Category categoryInfo in GetAllCategory())
                    {
                        if (flag && categoryInfo.Layer == layer)
                        {
                            flag = false;
                        }
                        if (flag || categoryInfo.ParentId == cateId)
                        {
                            flag = true;
                            categoryList.Add(categoryInfo);
                        }
                    }
                }
                else
                {
                    foreach (Category categoryInfo in GetAllCategory())
                    {
                        if (categoryInfo.ParentId == cateId)
                        {
                            flag = true;
                            categoryList.Add(categoryInfo);
                        }
                        else if (flag && categoryInfo.Layer == layer)
                        {
                            break;
                        }
                    }
                }
            }
            return categoryList;
        }

        /// <summary>
        /// 获得子分类列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="categoryList">分类列表</param>
        /// <returns></returns>
        public  List<Category> GetChildCategoryList(int cateId, List<Category> categoryList)
        {
            List<Category> list = new List<Category>();

            if (cateId > 0)
            {
                foreach (Category categoryInfo in categoryList)
                {
                    if (categoryInfo.ParentId == cateId)
                        list.Add(categoryInfo);
                }
            }
            return list;
        }

        /// <summary>
        /// 获得子分类列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="layer">级别</param>
        /// <param name="categoryList">分类列表</param>
        /// <returns></returns>
        public   List<Category> GetChildCategoryList(int cateId, int layer, List<Category> categoryList)
        {
            return GetChildCategoryList(cateId, layer, false, categoryList);
        }

        /// <summary>
        /// 获得子分类列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="layer">级别</param>
        /// <param name="isAllChildren">是否包括全部子节点</param>
        /// <param name="categoryList">分类列表</param>
        /// <returns></returns>
        public static List<Category> GetChildCategoryList(int cateId, int layer, bool isAllChildren, List<Category> categoryList)
        {
            List<Category> list = new List<Category>();

            if (cateId > 0)
            {
                bool flag = false;
                if (isAllChildren)
                {
                    foreach (Category categoryInfo in categoryList)
                    {
                        if (flag && categoryInfo.Layer == layer)
                        {
                            flag = false;
                        }
                        if (flag || categoryInfo.ParentId == cateId)
                        {
                            flag = true;
                            list.Add(categoryInfo);
                        }
                    }
                }
                else
                {
                    foreach (Category categoryInfo in categoryList)
                    {
                        if (categoryInfo.ParentId == cateId)
                        {
                            flag = true;
                            list.Add(categoryInfo);
                        }
                        else if (flag && categoryInfo.Layer == layer)
                        {
                            break;
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        public   void CreateCategory(Category categoryInfo)
        {
            if (categoryInfo.ParentId > 0)
            {
                List<Category> categoryList = GetAllCategory();
                Category parentCategoryInfo = categoryList.Find(x => x.CategoryID == categoryInfo.ParentId);//父分类
                categoryInfo.Layer = parentCategoryInfo.Layer + 1;
                categoryInfo.HasChild = 0;
                categoryInfo.Path = "";
                categoryInfo.EntityState = EntityState.Added;
                m_CategoryRepository.Insert(categoryInfo);
                m_UnitOfWork.SaveChanges();
                int categoryId = categoryInfo.CategoryID;

                //categoryInfo.CategoryID = categoryId;
                categoryInfo.Path = parentCategoryInfo.Path.TrimEnd() + "," + categoryId;
                categoryInfo.EntityState = EntityState.Modified;
                m_CategoryRepository.Update(categoryInfo);
                m_UnitOfWork.SaveChanges();

                if (parentCategoryInfo.HasChild == 0)
                {
                    parentCategoryInfo.HasChild = 1;
                    parentCategoryInfo.EntityState = EntityState.Modified;
                    m_CategoryRepository.Update(parentCategoryInfo);
                    m_UnitOfWork.SaveChanges();
                }
            }
            else
            {
                categoryInfo.Layer = 1;
                categoryInfo.HasChild = 0;
                categoryInfo.Path = "";
                categoryInfo.EntityState = EntityState.Added;
                m_CategoryRepository.Insert(categoryInfo);
                m_UnitOfWork.SaveChanges();
                int categoryId = categoryInfo.CategoryID;
                

                categoryInfo.CategoryID = categoryId;
                categoryInfo.Path = categoryId.ToString();
                categoryInfo.EntityState = EntityState.Modified;
                m_CategoryRepository.Update(categoryInfo);
                m_UnitOfWork.SaveChanges();
            }


            Cache_Category.ClearCategory();

          //  WriteMallAdminCategorySelectListCache(GetCategoryList());
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        public   bool UpdateCategory(Category categoryInfo, int oldParentId)
        {
            int result = 0;
            if (categoryInfo.ParentId != oldParentId)//父分类改变时
            {
                int changeLayer = 0;
                List<Category> categoryList = GetAllCategory();
                Category oldParentCategoryInfo = categoryList.Find(x => x.CategoryID == oldParentId);//旧的父分类
                if (categoryInfo.ParentId > 0)//非顶层分类时
                {
                    Category newParentCategoryInfo = categoryList.Find(x => x.CategoryID == categoryInfo.ParentId);//新的父分类
                    if (oldParentCategoryInfo == null)
                    {
                        changeLayer = newParentCategoryInfo.Layer - 1;
                    }
                    else
                    {
                        changeLayer = newParentCategoryInfo.Layer - oldParentCategoryInfo.Layer;
                    }
                    categoryInfo.Layer = newParentCategoryInfo.Layer + 1;
                    categoryInfo.Path = newParentCategoryInfo.Path.TrimEnd() + "," + categoryInfo.CategoryID;

                    if (newParentCategoryInfo.HasChild == 0)
                    {
                        newParentCategoryInfo.HasChild = 1;
                        newParentCategoryInfo.EntityState = EntityState.Modified;
                        m_CategoryRepository.Update(newParentCategoryInfo);
                        m_UnitOfWork.SaveChanges();
                        
                    }
                }
                else//顶层分类时
                {
                    changeLayer = 1 - oldParentCategoryInfo.Layer;
                    categoryInfo.Layer = 1;
                    categoryInfo.Path = categoryInfo.CategoryID.ToString();
                }

                if (oldParentId > 0 && categoryList.FindAll(x => x.ParentId == oldParentId).Count == 1)
                {
                    oldParentCategoryInfo.HasChild = 0;
                    oldParentCategoryInfo.EntityState = EntityState.Modified;
                    m_CategoryRepository.Update(oldParentCategoryInfo);
                    m_UnitOfWork.SaveChanges();                   
                }

                foreach (Category info in categoryList.FindAll(x => x.ParentId == categoryInfo.CategoryID))
                {
                    UpdateChildCategoryLayerAndPath(categoryList, info, changeLayer, categoryInfo.Path);
                }
            }
            categoryInfo.EntityState = EntityState.Modified;
            m_CategoryRepository.Update(categoryInfo);
            result =  m_UnitOfWork.SaveChanges();
            

            Cache_Category.ClearCategory();
            return result >= 1;
           // WriteMallAdminCategorySelectListCache(GetCategoryList());
        }

        /// <summary>
        /// 递归更新分类及其子分类的级别和路径
        /// </summary>
        /// <param name="categoryList">分类列表</param>
        /// <param name="categoryInfo">分类信息</param>
        /// <param name="changeLayer">变化的级别</param>
        /// <param name="parentPath">父路径</param>
        private  void UpdateChildCategoryLayerAndPath(List<Category> categoryList, Category categoryInfo, int changeLayer, string parentPath)
        {
            categoryInfo.Layer = categoryInfo.Layer + changeLayer;
            categoryInfo.Path = parentPath.TrimEnd() + "," + categoryInfo.CategoryID;
            categoryInfo.EntityState = EntityState.Modified;
            m_CategoryRepository.Update(categoryInfo);
            m_UnitOfWork.SaveChanges();

            foreach (Category info in categoryList.FindAll(x => x.ParentId == categoryInfo.CategoryID))
            {
                UpdateChildCategoryLayerAndPath(categoryList, info, changeLayer, categoryInfo.Path);
            }
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns>-3代表分类不存在,-2代表此分类下还有子分类未删除,-1代表此分类下还有属性分组未删除,0代表此分类下还有商品未删除,1代表删除成功</returns>
        public int DeleteCategoryById(int cateId)
        {
            List<Category> categoryList =GetAllCategory();
            Category categoryInfo = categoryList.Find(x => x.CategoryID == cateId);
            if (categoryInfo == null)
                return -3;
            if (categoryInfo.HasChild == 1)
                return -2;
            //to do 
            //if (GetAttributeGroupListByCateId(cateId).Count > 0)
            //    return -1;
            //to do 
            //if (AdminProducts.AdminGetCategoryProductCount(cateId) > 0)
            //    return 0;

            categoryInfo.EntityState = EntityState.Deleted;
            m_CategoryRepository.Delete(categoryInfo);
            m_UnitOfWork.SaveChanges();

            if (categoryInfo.Layer > 1 && categoryList.FindAll(x => x.ParentId == categoryInfo.ParentId).Count == 1)
            {
                Category parentCategoryInfo = categoryList.Find(x => x.CategoryID == categoryInfo.ParentId);
                parentCategoryInfo.HasChild = 0;
                parentCategoryInfo.EntityState = EntityState.Modified;
                m_CategoryRepository.Update(parentCategoryInfo);
                m_UnitOfWork.SaveChanges();                 
            }

            Cache_Category.ClearCategory();
            //to do 
            //BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_BRANDLIST + cateId);
            //BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_FILTERAANDVLIST + cateId);
            //BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_AANDVLISTJSONCACHE + cateId);

            //  WriteMallAdminCategorySelectListCache(GetCategoryList());
            return 1;
        }

        /// <summary>
        /// 获得分类的属性分组列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public   List<CategoryAttrGroups> GetAttributeGroupListByCateId(int cateId,int? categoryType)
        {
            return m_CategoryAttrGroupsRepository.GetAttributeGroupListByCateId(cateId, categoryType);             
        }

        /// <summary>
        /// 获得属性列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public List<CategoryAttr> GetAttributeListByAttrGroupId(int attrGroupId)
        {
            return m_CategoryAttrRepository.GetAttributeListByAttrGroupId(attrGroupId);
        }


        /// <summary>
        /// 通过分类id和属性分组名称获得分组id
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="name">分组名称</param>
        /// <returns></returns>
        public int GetAttributeGroupIdByCateIdAndName(int cateId, string name)
        {
            if (cateId < 1 || string.IsNullOrWhiteSpace(name))
                return 0;

            return m_CategoryAttrGroupsRepository.GetAttrGroupIdByCateIdAndName(cateId, name);
        }

        /// <summary>
        /// 创建属性分组
        /// </summary>
        public   bool CreateAttributeGroup(CategoryAttrGroups attributeGroupInfo)
        {
            attributeGroupInfo.EntityState = EntityState.Added;
            m_CategoryAttrGroupsRepository.Insert(attributeGroupInfo);
           return  m_UnitOfWork.SaveChanges()==1;             
        }

        /// <summary>
        /// 获得属性分组
        /// </summary>
        /// <param name="attrGroupId">属性分组id</param>
        public   CategoryAttrGroups GetAttributeGroupById(int attrGroupId)
        {
            return m_CategoryAttrGroupsRepository.GetAttributeGroupById(attrGroupId);             
        }
        /// <summary>
        /// 更新属性分组
        /// </summary>
        /// <param name="newAttributeGroupInfo">新属性分组</param>         
        public   bool UpdateAttributeGroup(CategoryAttrGroups attributeGroupInfo)
        {
            attributeGroupInfo.EntityState = EntityState.Modified;
            m_CategoryAttrGroupsRepository.Update(attributeGroupInfo);
          return   m_UnitOfWork.SaveChanges()==1;             
        }

        /// <summary>
        /// 后台获得列表排序
        /// </summary>
        /// <param name="sortColumn">排序列</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public string AdminGetBrandListSort(string sortColumn, string sortDirection)
        {
            if (string.IsNullOrWhiteSpace(sortColumn))
                sortColumn = "[displayorder]";
            if (string.IsNullOrWhiteSpace(sortDirection))
                sortDirection = "DESC";

            return string.Format("{0} {1} ", sortColumn, sortDirection);
        }
        /// <summary>
        /// 删除属性分组
        /// </summary>
        /// <param name="attrGroupId">属性分组id</param>
        /// <returns>0代表此分类下还有属性未删除,1代表删除成功</returns>
        public   int DeleteAttributeGroupById(int attrGroupId)
        {
            if (GetAttributeListByAttrGroupId(attrGroupId).Count > 0)
                return 0;
            CategoryAttrGroups cag = GetAttributeGroupById(attrGroupId);
            cag.EntityState = EntityState.Deleted;
            m_CategoryAttrGroupsRepository.Delete(cag);
            m_UnitOfWork.SaveChanges();             
            return 1;
        }


        /// <summary>
        /// 后台获得属性列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="sort">排序</param>
        /// <returns></returns>
        public List<CategoryAttrModel> AdminGetAttributeList(int cateId, string sort)
        {
            return m_CategoryAttrRepository.AdminGetAttributeList(cateId, sort);
        }

        /// <summary>
        /// 通过分类id和属性名称获得属性id
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="attributeName">属性名称</param>
        /// <returns></returns>
        public   int GetAttrIdByCateIdAndName(int cateId, string attributeName)
        {
            if (cateId < 1 || string.IsNullOrWhiteSpace(attributeName))
                return 0;
            return m_CategoryAttrRepository.GetAttrIdByCateIdAndName(cateId, attributeName);
        }

        /// <summary>
        /// 创建属性
        /// </summary>
        /// <param name="attributeInfo">属性信息</param>
        /// <param name="attributeGroupInfo">属性组信息</param>
        public  bool  CreateAttribute(CategoryAttr attributeInfo, CategoryAttrGroups attributeGroupInfo)
        {
            int result = 0;
            attributeInfo.EntityState = EntityState.Added;
            m_CategoryAttrRepository.Insert(attributeInfo);
            result = m_UnitOfWork.SaveChanges();
            CategoryAttrValues defaultValue = new CategoryAttrValues();
            defaultValue.AttrValue = "手动输入";
            defaultValue.Isinput = 1;
            defaultValue.AttrName = attributeInfo.Name;
            defaultValue.AttrDisplayOrder = attributeInfo.DisplayOrder;
            defaultValue.AttrShowType = attributeInfo.ShowType;
            defaultValue.AttrValueDisplayOrder = 0;
            defaultValue.AttrGroupId = attributeGroupInfo.AttrGroupId;
            defaultValue.AttrGroupName = attributeGroupInfo.Name;
            defaultValue.AttrGroupDisplayOrder = attributeGroupInfo.DisplayOrder;
            defaultValue.AttrId = attributeInfo.AttrId;
            defaultValue.EntityState = EntityState.Added;
            m_CategoryAttrValuesRepository.Insert(defaultValue);
            int tresult =  m_UnitOfWork.SaveChanges();
            return (result + tresult) == 2;
             

            //to do 
           
        }

        /// <summary>
        /// 获得属性
        /// </summary>
        /// <param name="attrId">属性id</param>
        /// <returns></returns>
        public   CategoryAttr GetAttributeById(int attrId)
        {
            return m_CategoryAttrRepository.GetAttributeById(attrId);
             
        }
        /// <summary>
        /// 更新属性
        /// </summary>
        /// <param name="newAttributeInfo">新属性</param>
        /// <param name="oldAttributeInfo">原属性</param>
        public   bool UpdateAttribute(CategoryAttr attributeInfo)
        {
            attributeInfo.EntityState = EntityState.Modified;
            m_CategoryAttrRepository.Update(attributeInfo);
            m_UnitOfWork.SaveChanges();
            string sql = string.Format(@"UPDATE CategoryAttrValues 
            SET AttrName='{0}',AttrDisplayOrder={1},AttrShowType={2} WHERE AttrId={3}",attributeInfo.Name,attributeInfo.DisplayOrder,attributeInfo.ShowType,attributeInfo.AttrId);
           int result =  m_CategoryAttrValuesRepository.ExecuteSqlCommand(sql);
            return result >= 1;
            //to do
            
        }

        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="attrId">属性id</param>
        /// <returns>0代表此属性下还有属性值未删除,1代表删除成功</returns>
        public   int DeleteAttributeById(int attrId)
        {
            if (GetAttributeValueListByAttrId(attrId).Count > 1)
                return 0;
            CategoryAttr attributeInfo = GetAttributeById(attrId);
            attributeInfo.EntityState = EntityState.Deleted;
            m_CategoryAttrRepository.Delete(attributeInfo);
            m_UnitOfWork.SaveChanges();                      
            return 1;
        }

        /// <summary>
        /// 获得属性值列表
        /// </summary>
        /// <param name="attrId">属性id</param>
        /// <returns></returns>
        public   List<CategoryAttrValues> GetAttributeValueListByAttrId(int attrId)
        {
           return  m_CategoryAttrValuesRepository.GetAttributeValueListByAttrId(attrId);            
        }

        public List<CategoryAttrValues> GetAttributeValueListByAttrId(List<int> attrIds)
        {
            return m_CategoryAttrValuesRepository.GetAttributeValueListByAttrId(attrIds);
        }

        /// <summary>
        /// 获得分类属性列表json缓存
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public   string GetCategoryAAndVListJsonCache(int cateId)
        {           
            string jsonCache = string.Empty;
            if (jsonCache == null)
            {
                StringBuilder json = new StringBuilder("[");

                List<KeyValuePair<CategoryAttr, List<CategoryAttrValues>>> itemList = GetCategoryAAndVList(cateId);
                foreach (KeyValuePair<CategoryAttr, List<CategoryAttrValues>> item in itemList)
                {
                    CategoryAttr attributeInfo = item.Key;
                    json.AppendFormat("{0}\"attrId\":\"{1}\",\"name\":\"{2}\",\"attrValueList\":[", "{", attributeInfo.AttrId, attributeInfo.Name);
                    foreach (CategoryAttrValues attributeValueInfo in item.Value)
                    {
                        json.AppendFormat("{0}\"attrValueId\":\"{1}\",\"attrValue\":\"{2}\",\"isInput\":\"{3}\"{4},", "{", attributeValueInfo.AttrValueId, attributeValueInfo.AttrValue, attributeValueInfo.Isinput, "}");
                    }
                    if (item.Value.Count > 0)
                        json.Remove(json.Length - 1, 1);

                    json.AppendFormat("]{0},", "}");
                }
                if (itemList.Count > 0)
                    json.Remove(json.Length - 1, 1);

                json.Append("]");

                jsonCache = json.ToString();
             
            }

            return jsonCache;
        }

        /// <summary>
        /// 获得分类属性列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public   List<KeyValuePair<CategoryAttr, List<CategoryAttrValues>>> GetCategoryAAndVList(int cateId)
        {
            List<KeyValuePair<CategoryAttr, List<CategoryAttrValues>>> itemList = new List<KeyValuePair<CategoryAttr, List<CategoryAttrValues>>>();

            List<CategoryAttr> attributeList = GetAttributeListByCateId(cateId);
            foreach (CategoryAttr attributeInfo in attributeList)
            {
                List<CategoryAttrValues> attributeValueList = GetAttributeValueListByAttrId(attributeInfo.AttrId);
                itemList.Add(new KeyValuePair<CategoryAttr, List<CategoryAttrValues>>(attributeInfo, attributeValueList));
            }

            return itemList;
        }
        /// <summary>
        /// 获得属性列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public   List<CategoryAttr> GetAttributeListByCateId(int cateId)
        {
              return m_CategoryAttrRepository.GetAttributeListByCateId(cateId);
        }


        /// <summary>
        /// 获得属性的可选值列表
        /// </summary>
        /// <param name="attrId">属性id</param>
        /// <returns></returns>
        public   List<CategoryAttrValues> GetAttributeSelectValueListByAttrId(int attrId)
        {
            List<CategoryAttrValues> attributeValueList1 = GetAttributeValueListByAttrId(attrId);
            if (attributeValueList1.Count < 2)
                return new List<CategoryAttrValues>();

            List<CategoryAttrValues> attributeValueList2 = new List<CategoryAttrValues>(attributeValueList1.Count - 1);
            foreach (CategoryAttrValues attributeValueInfo in attributeValueList1)
            {
                if (attributeValueInfo.Isinput == 0)
                    attributeValueList2.Add(attributeValueInfo);
            }
            return attributeValueList2;
        }

        /// <summary>
        /// 通过属性id和属性值获得属性值的id
        /// </summary>
        /// <param name="attrId">属性id</param>
        /// <param name="attrValue">属性值</param>
        /// <returns></returns>
        public   int GetAttributeValueIdByAttrIdAndValue(int attrId, string attrValue)
        {
            if (attrId < 1 || string.IsNullOrWhiteSpace(attrValue))
                return 0;
            return m_CategoryAttrValuesRepository.GetAttributeValueIdByAttrIdAndValue(attrId, attrValue);
        }

        /// <summary>
        /// 创建属性值
        /// </summary>
        public   bool CreateAttributeValue(CategoryAttrValues attributeValueInfo)
        {
            // BrnMall.Data.Categories.CreateAttributeValue(attributeValueInfo);
            attributeValueInfo.EntityState = EntityState.Added;
            m_CategoryAttrValuesRepository.Insert(attributeValueInfo);
            int result =m_UnitOfWork.SaveChanges();
            CategoryAttr attributeInfo = GetAttributeById(attributeValueInfo.AttrId);
            if (attributeInfo.Isfilter == 1)
            {
            //      BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            }
            return result == 1;
           // BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }


        /// <summary>
        /// 获得属性值
        /// </summary>
        /// <param name="attrValueId">属性值id</param>
        /// <returns></returns>
        public   CategoryAttrValues GetAttributeValueById(int attrValueId)
        {
            return m_CategoryAttrValuesRepository.GetAttributeValueById(attrValueId); 
        }
        /// <summary>
        /// 更新属性值
        /// </summary>
        public  bool UpdateAttributeValue(CategoryAttrValues attributeValueInfo)
        {
            int result = 0;
            attributeValueInfo.EntityState = EntityState.Modified;
            m_CategoryAttrValuesRepository.Update(attributeValueInfo);
            result = m_UnitOfWork.SaveChanges();
             
            CategoryAttr attributeInfo = GetAttributeById(attributeValueInfo.AttrId);
            if (attributeInfo.Isfilter == 1)
            {
                //BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            }
            //BrnMall.Core.BMACache.Remove(CacheKeys.MALL_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
            return result == 1;
        }
        /// <summary>
        /// 删除属性值
        /// </summary>
        /// <param name="attrValueId">属性值id</param>
        /// <returns>-2代表此属性值不存在,-1代表此属性值为输入属性值,0代表此属性值下还有商品未删除,1代表删除成功</returns>
        public   int DeleteAttributeValueById(int attrValueId)
        {
            CategoryAttrValues attributeValueInfo = GetAttributeValueById(attrValueId);
            if (attributeValueInfo == null)
                return -2;
            if (attributeValueInfo.Isinput == 1)
                return -1;
            //to do
           // if (AdminProducts.AdminGetAttrValueProductCount(attrValueId) > 0)
            //    return 0;
            CategoryAttr attributeInfo = GetAttributeById(attributeValueInfo.AttrId);
            attributeValueInfo.EntityState = EntityState.Deleted;
            m_CategoryAttrValuesRepository.Delete(attributeValueInfo);
            m_UnitOfWork.SaveChanges();             
            if (attributeInfo.Isfilter == 1)
            {
                //to do 
               
            }
            //to do 
             
            return 1;
        }
    }
}
